<?php


namespace app\queuebylinklist\controller;


class Queue
{
   private  $header = null;
   public function __construct($data)
   {
        $this->header = new Node($data);
   }
    // 判断队列是否为空
    public function isEmpty()
    {
        return $this->header->next === null;
    }
    // 入队，在队尾加入数据
    public function enqueue($data)
    {
        $node = new Node($data);
        $current = $this->header;
        while (!is_null($current->next)){
            $current = $current->next;
        }
        $current->next = $node;
    }
    // 出队，返回并移除队首数据。队空不可出队
    public function dequeue()
    {
        if($this->isEmpty()){
            return false;
        }
        $this->header->next = $this->header->next->next;
    }
    // 清空队列
    public function clear()
    {
        $this->header = null;
    }
    // 显示队列中的元素
    public function show()
    {
        $current = $this->header;
        while (!is_null($current->next)){
            echo $current->next->data.PHP_EOL;
            $current = $current->next;
        }
    }
}